def check(idx, i):
    for j in range(idx):
        if abs(j - idx) == abs(q[j] - i):
            return False
    return True

def queen(idx, n):
    if idx == n:
        for i in range(n):
            for j in range(n):
                if q[i] == j:
                    print("Q", end=" ")
                else:
                    print(".", end=" ")
            print()
        print()
        return
    for i in range(n):
        if b[i]==False and check(idx, i):
            q[idx] = i
            b[i]=True
            queen(idx + 1, n)
            b[i]=False
    return


n = 4
b= [ False for i in range(n)]
q = [-1 for i in range(n)]
queen(0, n)